import com.mochimedia.ui.*;

class com.mochimedia.ui.BButton extends Component implements IComponent {
    
    private var _type:String = "button";
    
    private var _textLabel:String = "";
    private var _labelAlign:Number;
    private var _alt:String = "";
    private var _icon:Object;
    
    private var _button_mc:MovieClip;
    private var _btn:MovieClip;
	private var _btn_tf:TextField;
	private var _btn_icon:MovieClip;
    private var _btn_inactive:MovieClip;
    private var _btn_selected:MovieClip;
    private var _tabMode:Boolean = false;
    private var _groupMode:Boolean = false;

    public function get button():MovieClip { return _btn; }
    public function get textLabel():String { return _textLabel; }
    
    public function get alt ():String { return _alt; }
    public function set alt (value:String):Void { _alt = value; }

    public var value:String = "";
    
    private var _draggable:Boolean;
    public function get draggable():Boolean { return _draggable; }
    
    private var _valueX:Number = 0;
    public function get valueX():Number { return _valueX; }
    
    private var _valueY:Number = 0;
    public function get valueY():Number { return _valueY; }
    
    public function set valueX(value:Number):Void {
        _valueX = Math.max(0, Math.min(1, value));
        _mc._x = _valueX * (parentCell.width - width);
    }
    
    public function set valueY(value:Number):Void {
        _valueY = Math.max(0, Math.min(1, value));
        _mc._y = _valueY * (parentCell.height - height);
    }
    
    //
    //
    public function BButton (container:MovieClip, label:Object, labelAlign:Number, width:Number, height:Number, tabMode:Boolean, groupMode:Boolean, dragMode:Boolean, position:Position, style:Style) {
        
        init (container, label, labelAlign, width, height, tabMode, groupMode, dragMode, position, style);
        
        if (_container != null) create();
        
    }
    
    //
    //
    private function init (container:MovieClip, label:Object, labelAlign:Number, width:Number, height:Number, tabMode:Boolean, groupMode:Boolean, dragMode:Boolean, position:Position, style:Style):Void {
        
        super.init(container, position, style);
        
        if (typeof(label) == "string") _textLabel = String(label);
        if (label instanceof Clip) _icon = label;
        else if (label != null) _icon = Array(label);
        
        _labelAlign = (labelAlign != null) ? labelAlign : Position.ALIGN_CENTER;
        
        _width = width;
        _height = height;
        _tabMode = tabMode;
        _groupMode = groupMode;
        _draggable = dragMode;
        
    }
    
    //
    //
    public function create ():Void {

        super.create();
        
        if (_textLabel.length > 0) _button_mc = Create.button(_mc, _textLabel, _labelAlign, _width, _height, _tabMode, _groupMode, _style);
        else if (_icon instanceof Clip) {
            _button_mc = Create.iconButton(_mc, Clip(_icon), Position.ALIGN_LEFT, _width, _height, _tabMode, _groupMode, _style);
            _textLabel = (Clip(_icon).value.length > 0) ? Clip(_icon).value : Clip(_icon).alt;
        } else {
			if (isNaN(_width)) _width = 32;
			if (isNaN(_height)) _height = 32;
			_button_mc = Create.hitArea(_mc, Array(_icon), _width, _height, _style);
        }
        
        
        _btn = _button_mc.btn;
		_btn_tf = _button_mc["_buttontext0"];
		_btn_icon = _mc["_icon_1"];

        _btn.tabIndex = id;
        _btn_inactive = _button_mc.btn_inactive;
        _btn_selected = _button_mc.btn_selected;
        
        if (isNaN(_width)) _width = _button_mc._width;
        if (isNaN(_height)) _height = _button_mc._height;
        
        connectButton(_btn, _draggable);
        
        addEventListener(EVENT_ROLLOVER, this, rollover);
        addEventListener(EVENT_ROLLOUT, this, rollout);
        
        if (draggable) {
            
            addEventListener(EVENT_PRESS, this, startDrag);
            addEventListener(EVENT_RELEASE, this, stopDrag);
            addEventListener(EVENT_DRAG, this, onDrag);
            
        }

    }
    
    //
    //
    public function resize (width:Number, height:Number):Void {
        
        _width = width;
        _height = height;
        
        _mc.clear();
        
        delete _btn;
        delete _btn_inactive;
        delete _btn_selected;
        
        _button_mc.removeMovieClip();

        delete _button_mc;
        
        _created = false;
        
        create();
        
    }
    
    //
    //
    public function enable ():Void {
        
        super.enable();
        
        _btn._visible = true;
		_btn_tf._visible = true;
		_btn_icon._visible = true;
        _btn_inactive._visible = false;
        _btn_selected._visible = false;
        
    }
    
    //
    //
    public function disable ():Void {

        super.disable();
        
        _btn._visible = false;
		_btn_tf._visible = false;
		_btn_icon._visible = false;
        _btn_inactive._visible = true;
        _btn_selected._visible = false;
		Tagtip.hideTag();
        
    }
    
    //
    //
    public function select ():Void {

        if (_enabled) {
            
            _btn._visible = false;
            _btn_inactive._visible = false;
            _btn_selected._visible = true;
            
            dispatchEvent(EVENT_FOCUS);
            
        }
        
    }
    
    //
    //
    public function deselect ():Void {
        
        if (_enabled) {

            _btn._visible = true;
            _btn_inactive._visible = false;
            _btn_selected._visible = false;     
            
            dispatchEvent(EVENT_BLUR);
			Tagtip.hideTag();
            
        }
        
    }
    
    //
    //
    function rollover ():Void {

        _btn._alpha = 100;
        if (_alt.length > 0) Tagtip.showTag(_alt);

    }
    
    //
    //
    function rollout ():Void {
        
        _btn._alpha = 0;	
        
        Tagtip.hideTag();
        
    }
    
    //
    //
    function startDrag ():Void {
        
        var self:Object = this;

        _mc.onEnterFrame = function ():Void {
            
            if (this._x != this.dragX || this._y != this.dragY) {
                
                this.dragX = this._x;
                this.dragY = this._y;
                
                self.dispatchEvent(Component.EVENT_DRAG);
                
            }
            
        }
        
        _mc.startDrag(false, 0, 0, parentCell.width - width, parentCell.height - height);
        
    }
    
    //
    //
    function onDrag ():Void {
        
        if (parentCell.width - width <= 0) _valueX = 1;
        else _valueX = _mc._x / (parentCell.width - width);
        
        if (parentCell.height - height <= 0) _valueY = 1;
        else _valueY = _mc._y / (parentCell.height - height);
        
        dispatchEvent(EVENT_CHANGE);
        
    }
    
    //
    //
    function stopDrag ():Void {
        
        delete _mc.onEnterFrame;
        
        _mc.stopDrag();
        
    }
    
}